package com.dragon.utilslibrary.dblib.clipher;

import android.content.Context;

import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteOpenHelper;

public class SQLMMHelper extends SQLiteOpenHelper {

    //数据库版本
    public static int VERSION = 1;
    private static SQLMMHelper instant = null;
    private SQLiteDatabase db;
    private static String userId;
    Context context;

    public SQLMMHelper(Context context, String id, SQLiteDatabase.CursorFactory factory, int version) {
        super(new DatabaseContext(context), id+ ".db", factory, version);
        //不可忽略的  进行so库加载

        this.context = context;
        userId = id;
    }

    public SQLMMHelper(Context context, String id) {
        this(context, id, null, VERSION);

    }
    public synchronized static SQLMMHelper getInstant(Context context, String id) {
        if (instant == null || id != userId) {
            instant = new SQLMMHelper(context, id);
        }
        return instant;
    }

    /**
     * 打开数据库
     * @return
     */
    public synchronized SQLiteDatabase openDB(String DBPassword) {
        try {
            if (instant == null) {
                return null;
            }
            if (db == null || !db.isOpen()) {
                db = instant.getWritableDatabase(DBPassword);
            } else {
//                db.close();
//                db = null;
//                db = instant.getWritableDatabase(db_mm);
            }
        } catch (Exception e) {
            return db;
        }
        return db;
    }

    /**
     * 关闭数据库
     */
    public synchronized void closeDB() {
//        if (db != null) {
//            db.close();
//            db = null;
//        }
    }

    /**
     * 创建数据库
     *
     * @param db
     */
    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

}
